<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
    <#list table.commonFields as field><#--生成公共字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
    </#list>
    <#list table.fields as field>
    <#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
    </#if>
    </#list>
    </resultMap>
    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        <#list table.commonFields as field>
            ${field.columnName},
        </#list>
        ${table.fieldNames}
    </sql>

    <!-- 根据ID查询 -->
    <select id="selectById" resultType="${package.Entity}.${entity}">
        SELECT
        <#list table.commonFields as field>
            ${field.columnName},
        </#list>
            ${table.fieldNames}
        FROM ${table.name}
        WHERE id = ${'#'}{id}
    </select>

    <!-- 查询列表 -->
    <select id="selectList" resultType="${package.Entity}.${entity}">
        SELECT
        <#list table.commonFields as field>
            ${field.columnName},
        </#list>
            ${table.fieldNames}
        FROM ${table.name}
        <where>
        <#list table.fields as field>
            <#if field.propertyType?upper_case == "STRING">
            <if test="${field.propertyName} != null and ${field.propertyName} != ''">
                AND ${field.name} = ${'#'}{${field.propertyName}}
            </if>
            <#else>
            <if test="${field.propertyName} != null">
                AND ${field.name} = ${'#'}{${field.propertyName}}
            </if>
            </#if>
        </#list>
        </where>
    </select>

    <!-- 插入 -->
    <insert id="insert">
        INSERT INTO ${table.name}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list table.commonFields as field>
                <if test="${field.propertyName} != null">${field.name},</if>
            </#list>
            <#list table.fields as field>
                <if test="${field.propertyName} != null">${field.name},</if>
            </#list>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list table.commonFields as field>
                <if test="${field.propertyName} != null">${'#'}{${field.propertyName}},</if>
            </#list>
            <#list table.fields as field>
                <if test="${field.propertyName} != null">${'#'}{${field.propertyName}},</if>
            </#list>
        </trim>
    </insert>

    <!-- 更新 -->
    <update id="update">
        UPDATE ${table.name}
        <set>
        <#list table.fields as field>
            <#if field.propertyType?upper_case == "STRING">
            <if test="${field.propertyName} != null and ${field.propertyName} != ''">
                ${field.name} = ${'#'}{${field.propertyName}},
            </if>
            <#else>
            <if test="${field.propertyName} != null">
                ${field.name} = ${'#'}{${field.propertyName}},
            </if>
            </#if>
        </#list>
        </set>
        WHERE id = ${'#'}{id}
    </update>

    <!-- 批量插入 -->
    <insert id="batchInsert">
        <foreach collection="list" item="item" separator=";">
            INSERT INTO ${table.name}
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <#list table.commonFields as field>
                    <if test="${field.propertyName} != null">${field.name},</if>
                </#list>
                <#list table.fields as field>
                    <if test="${field.propertyName} != null">${field.name},</if>
                </#list>
            </trim>
            VALUES
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <#list table.commonFields as field>
                    <if test="${field.propertyName} != null">${'#'}{${field.propertyName}},</if>
                </#list>
                <#list table.fields as field>
                    <if test="${field.propertyName} != null">${'#'}{${field.propertyName}},</if>
                </#list>
            </trim>
        </foreach>
    </insert>

    <!-- 批量更新 -->
    <update id="batchUpdate">
        <foreach collection="list" item="item" separator=";">
            UPDATE ${table.name}
            <set>
            <#list table.fields as field>
                <#if field.propertyType?upper_case == "STRING">
                <if test="item.${field.propertyName} != null and item.${field.propertyName} != ''">
                    ${field.name} = ${'#'}{item.${field.propertyName}},
                </if>
                <#else>
                <if test="item.${field.propertyName} != null">
                    ${field.name} = ${'#'}{item.${field.propertyName}},
                </if>
                </#if>
            </#list>
            </set>
            WHERE id = ${'#'}{item.id}
        </foreach>
    </update>

</mapper>
